package org.metasketch.editor.links;

import java.util.ArrayList;
import java.util.List;

import org.metasketch.diagram.javafx.graphics.BaseGraphic;

public class GraphicalLink implements ILink {

	SemanticConfig semanticConfig = null;

	List<GraphicalLinkConfig> graphicalLinkConfigs = new ArrayList<GraphicalLinkConfig>();

	String style;

	public GraphicalEndConfig isPossibleFromGraphicType(String graphicType) {

		if (graphicalLinkConfigs.size() == 0)
			return new GraphicalEndConfig();

		GraphicalEndConfig temp = null;
		for (GraphicalLinkConfig linkConfig : graphicalLinkConfigs) {
			temp = linkConfig.isSourceOrTarget(graphicType);
			if (temp != null)
				return temp;
		}

		return null;

	}

	public EndType getEndType(Object source, Object target) {
		return getFinishEndTypeFromGraphicType((String) source, (String) target);
	}
	
	public EndType getFinishEndTypeFromGraphicType(String startGraphicType,
			String finishGraphicType) {

		if (graphicalLinkConfigs.size() == 0)
			return EndType.TARGET;

		EndType endType = null;

		for (GraphicalLinkConfig linkConfig : graphicalLinkConfigs) {
			endType = linkConfig.getFinishEndTypeFromGraphicType(
					startGraphicType, finishGraphicType);
			if (endType != null)
				return endType;
		}
		return null;

	}

	public EndType getStrictFinishEndTypeFromGraphicType(
			String startGraphicType, String finishGraphicType) {

		if (graphicalLinkConfigs.size() == 0)
			return EndType.TARGET;

		EndType endType = null;

		for (GraphicalLinkConfig linkConfig : graphicalLinkConfigs) {
			endType = linkConfig.getStrictFinishEndTypeFromGraphicType(
					startGraphicType, finishGraphicType);
			if (endType != null)
				return endType;
		}
		return null;

	}

	public EndType getProperFinishEndTypeFromGraphicType(
			String startGraphicType, String finishGraphicType) {

		EndType endType = null;
		endType = getStrictFinishEndTypeFromGraphicType(startGraphicType,
				finishGraphicType);

		if (endType == null)
			endType = getFinishEndTypeFromGraphicType(startGraphicType,
					finishGraphicType);

		return endType;
	}

	public GraphicalLinkConfig getLinkConfigFromGraphicType(
			String startGraphicType, String finishGraphicType) {

		if (graphicalLinkConfigs.size() == 0)
			return null;

		// EndType endType = null;

		for (GraphicalLinkConfig linkConfig : graphicalLinkConfigs) {
			if (linkConfig.getSource().getTypes().contains(startGraphicType)
					&& linkConfig.getTarget().getTypes()
							.contains(finishGraphicType))
				return linkConfig;
		}
		return null;

	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public SemanticConfig getSemanticConfig() {
		return semanticConfig;
	}

	public void setSemanticConfig(SemanticConfig semanticConfig) {
		this.semanticConfig = semanticConfig;
	}

	public List<GraphicalLinkConfig> getGraphicalLinkConfigs() {
		return graphicalLinkConfigs;
	}

	public void setGraphicalLinkConfigs(List<GraphicalLinkConfig> linkConfigs) {
		this.graphicalLinkConfigs = linkConfigs;
	}

	@Override
	public GraphicalEndConfig isPossible(BaseGraphic source) {
		if (source == null)
			return isPossibleFromGraphicType(null);
		else
			return isPossibleFromGraphicType(source.getGraphicNode().getType());
	}

	@Override
	public EndType getFinishEndType(BaseGraphic source, BaseGraphic target) {
		String sourceType = null;
		String targetType = null;

		if (source != null)
			sourceType = source.getGraphicNode().getType();

		if (target != null)
			targetType = target.getGraphicNode().getType();

		return getFinishEndTypeFromGraphicType(sourceType, targetType);
	}

	@Override
	public EndType getStrictFinishEndType(BaseGraphic source, BaseGraphic target) {

		String sourceType = null;
		String targetType = null;

		if (source != null)
			sourceType = source.getGraphicNode().getType();

		if (target != null)
			targetType = target.getGraphicNode().getType();

		return getStrictFinishEndTypeFromGraphicType(sourceType, targetType);
	}

	@Override
	public EndType getProperFinishEndType(BaseGraphic source, BaseGraphic target) {

		String sourceType = null;
		String targetType = null;

		if (source != null)
			sourceType = source.getGraphicNode().getType();

		if (target != null)
			targetType = target.getGraphicNode().getType();

		return getProperFinishEndTypeFromGraphicType(sourceType, targetType);
	}

	@Override
	public GraphicalLinkConfig getLinkConfig(BaseGraphic source,
			BaseGraphic target) {

		String sourceType = null;
		String targetType = null;

		if (source != null)
			sourceType = source.getGraphicNode().getType();

		if (target != null)
			targetType = target.getGraphicNode().getType();

		return getLinkConfigFromGraphicType(sourceType, targetType);
	}

}
